import {
  reqGetOrderList
} from '../../../api/order'

Page({
  // 页面的初始数据
  data: {
    orderList: [],
    total: 0,
    loading: true,
    isFinish: false,
    page: 1,
    limit: 10,
  },
  onLoad() {
    this.getOrderList()
  },
  // 获取订单列表
  async getOrderList() {
    this.data.isLoading = true
    const {
      page,
      limit
    } = this.data
    const {
      code,
      data
    } = await reqGetOrderList({
      page,
      limit
    })
    if (code === 200) {
      this.setData({
        orderList: [...this.data.orderList, ...data.records],
        total: data.total,
        loading: false
      })
      this.data.isLoading = false
    }
  },
  // 订单详情
  goOrderDetail(event) {
    console.log(event, 'goOrderDetail');
    const {
      orderno
    } = event.currentTarget.dataset

    wx.navigateTo({
      url: `/modules/orderPayModule/pages/order/detail/detail?orderno=${orderno}`,
    })
  },
  // 上拉加载更多
  onReachBottom() {
    if (this.data.total === this.data.orderList.length) {
      this.setData({
        isFinish: true
      })
      return
    }
    // 判断是否加载完毕，如果 isLoading 等于 true
    // 说明数据还没有加载完毕，不加载下一页数据
    if (this.data.isLoading) return
    this.setData({
      page: this.data.page + 1,
    })
    this.getOrderList()
  },
  // 下拉刷新
  onPullDownRefresh() {
    // 重置数据
    this.setData({
      orderList: [], // 商品列表数据
      isFinish: false, // 判断数据是否加载完毕
      page: 1,
      limit: 10,
      total: 0,
      loading: true
    })
    // 重新发送请求
    this.getOrderList()
    // 停止下拉刷新
    wx.stopPullDownRefresh()
    // 提示信息
    wx.flowerTipToast({
      title: '刷新成功',
      icon: 'success'
    })
  }
})